package com.itheima.controller.backend.security;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;

import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Autowired
    private CheckItemService checkItemService;
    /**
     * 新增检查项
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        boolean succcess = checkItemService.addCheckItem(checkItem);
        if (succcess){
            return new Result(true, "添加成功");
        }else{
            return new Result(false, "添加失败");
        }
    }
    /**
     * 查询所有检查项
     */
    @GetMapping("/findAll")
    public List<CheckItem> findAll(){
        return checkItemService.findAll();
    }
    /**
     * 根据id删除检查项
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam Integer id){
        boolean success = checkItemService.delete(id);
        if (success){
            return new Result(true, "删除成功");
        }else{
            return new Result(false, "删除失败");
        }
    }
    /**
     * 根据id查询检查项
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id) {
        CheckItem checkItem = checkItemService.findById(id);
        if (checkItem != null) {
            return new Result(true, "查询成功", checkItem);
        } else {
            return new Result(false, "未找到检查项");
        }
    }

    /**
     * 修改检查项
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        boolean success = checkItemService.updateCheckItem(checkItem);
        return new Result(success, success ? "修改成功" : "修改失败");
    }
    /**
     * 分页查询
     */
    @PostMapping("/findPageByCode")
    public PageResult findPageByCode(@RequestBody QueryPageBean queryPageBean) {
        Pageable pageable = PageRequest.of(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        return checkItemService.findPageByCode(pageable, queryPageBean.getQueryString());
    }
    @PostMapping("/findPageByName")
    public PageResult findPageByName(@RequestBody QueryPageBean queryPageBean) {
        Pageable pageable = PageRequest.of(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        return checkItemService.findPageByName(pageable, queryPageBean.getQueryString());
    }
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Pageable pageable = PageRequest.of(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        if (queryString != null && !queryString.isEmpty()) {
            // 判断是否为 code 查询（可自定义逻辑）
            if (isCodeQuery(queryString)) {
                return checkItemService.findPageByCode(pageable, queryString);
            } else {
                return checkItemService.findPageByName(pageable, queryString);
            }
        }
        // 当 queryString 为空时，调用默认分页查询方法
        else
        {
            return findPageDefault(pageable);
        }
    }

    private PageResult findPageDefault(Pageable pageable) {
        return checkItemService.findPageDefault(pageable);
    }

    // 辅助方法：判断是否为 code 查询（示例逻辑）
    private boolean isCodeQuery(String keyword) {
        // 可以根据业务逻辑判断是否是 code 查询，例如只包含字母数字时认为是 code
        return keyword.matches("[a-zA-Z0-9]+");
    }

}
